package com.lq.service;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lq.common.Constants;
import com.lq.common.SqlConstant;
import com.lq.dao.DataDao;
import com.lq.utils.CommonUtil;
import com.lq.utils.jdbc.Jdbc;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DataService {

    public static Logger log = Logger.getLogger(DataService.class);

    public static Logger realTime = Logger.getLogger("realtime");
    public static Logger logUnique = Logger.getLogger("unique");
    public static Logger logHost = Logger.getLogger("host");
    public static Logger logDevice = Logger.getLogger("device");
    public static Logger logRecord = Logger.getLogger("record");
    public static Logger logGps = Logger.getLogger("gps");
    public static Logger logCXRecord = Logger.getLogger("cxrecord");
    public static Logger logTask = Logger.getLogger("task");
    public static Logger logRepalr = Logger.getLogger("repalr");

    /**
     * 查询实时数据（甘肃红十字血液中心专用）
     *
     * @param serverIp
     * @param reqTimeout
     * @param isOfficial
     * @param companyCode
     * @param areaCode
     */
    public void getRealTimeListData(String serverIp, int reqTimeout, boolean isOfficial, String companyCode, String areaCode) {
        try {
            //发起请求
            Map map = new HashMap();
            map.put("companyCode", companyCode);
            map.put("areaCode", areaCode);
            map.put("requstTime", System.currentTimeMillis());
            HttpResponse httpResponse = HttpRequest.get(serverIp + Constants.GET_REAL_TIME_LIST_DATA)
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                    .form(map)
                    .timeout(6 * 1000)
                    .execute();
            if (httpResponse != null && httpResponse.getStatus() == 200) {
                JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                if (resultObj.getInteger("code") == 200) {
                    JSONArray jsonArray = resultObj.getJSONArray("data");
                    if (jsonArray != null && jsonArray.size() > 0) {
                        realTime.info("==> 获取到实时数据" + jsonArray.size() + "条");
                        DataDao.insertRealTimeData(jsonArray, companyCode, areaCode);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            realTime.info("==> 调用服务接口==> getRealTimeListData发生异常==============》\n" + CommonUtil.e(e));
        }
    }

    /**
     * 查询设备唯一标识
     *
     * @param serverIp
     * @param reqTimeout
     */
    public void getEquipmentUniqueListData(String serverIp, int reqTimeout, boolean isOfficial) {
        try {
            //发起请求
            HttpResponse httpResponse = HttpRequest.get(serverIp + Constants.POST_USE_EQUIPMENT_UNIQUE_LIST_DATA)
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                    .timeout(reqTimeout)
                    .execute();
            if (httpResponse.getStatus() == 200) {
                JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                if (resultObj.getInteger("code") == 200) {
                    JSONArray jsonArray = resultObj.getJSONArray("data");
                    if (jsonArray != null && jsonArray.size() > 0) {
                        DataDao.insertEquipmentUnique(jsonArray);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logUnique.info("==> 调用服务接口==> getEquipmentUniqueListData发生异常==============》\n" + CommonUtil.e(e));
        }
    }


    /**
     * 获取设备信息数据
     *
     * @param serverIp
     * @param reqTimeout
     * @param terraceType
     * @param companyKey
     */
    public void getEquipmentListData(String serverIp, int reqTimeout, String terraceType, String companyKey, boolean isOfficial) {
        try {
            String companys = "";
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select company_code  from  company where length(company_code) != 0 and trusteeship = '1' and company_mark_key = ? ";
                List<ListOrderedMap> list = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = list.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            map.put("companys", companys);
            //发起请求
            HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_EQUIPMENT_LIST_DATA)
                    .body(JSONObject.toJSONString(map), "application/json")
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                    .timeout(reqTimeout)
                    .execute();
            if (httpResponse.getStatus() == 200) {
                JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                if (resultObj.getInteger("code") == 200) {
                    JSONArray jsonArray = resultObj.getJSONArray("data");
                    if (jsonArray != null && jsonArray.size() > 0) {
                        DataDao.insertEquipment(jsonArray);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logDevice.info("==> 调用服务接口==> getEquipmentListData发生异常==============》\n" + CommonUtil.e(e));
        }
    }


    /**
     * 获取设备主机数据
     *
     * @param serverIp
     * @param reqTimeout
     * @param terraceType
     * @param companyKey
     */
    public void getEquipmentHostListData(String serverIp, int reqTimeout, String terraceType, String companyKey, boolean isOfficial) {
        try {
            String companys = "";
            List<ListOrderedMap> companyList = new ArrayList<>();
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select c.company_code, ca.area_code from  company c left join company_area ca on ca.company_code = c.company_code where length(c.company_code) != 0 and c.trusteeship = '1' and c.company_mark_key = ? ";
                companyList = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = companyList.stream().map(t -> t.get("company_code").toString()).distinct().collect(Collectors.joining(","));
            } else {
                String findSQL = "select company_code from company where length(company_code) != 0";
                companyList = Jdbc.find(findSQL, new Object[]{});
                companys = companyList.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            map.put("companys", companys);
            //发起请求
            HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_EQUIPMENT_HOST_LIST_DATA)
                    .body(JSONObject.toJSONString(map), "application/json")
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                    .timeout(reqTimeout)
                    .execute();
            if (httpResponse.getStatus() == 200) {
                JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                if (resultObj.getInteger("code") == 200) {
                    JSONArray jsonArray = resultObj.getJSONArray("data");
                    if (jsonArray != null && jsonArray.size() > 0) {
                        DataDao.insertManageHost(jsonArray, terraceType, companyList);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logHost.info("==> 调用服务接口==> getEquipmentHostListData发生异常==============》" + Constants.LINE_SEPARATOR + CommonUtil.e(e));
        }
    }


    /**
     * 获取设备信息数据
     *
     * @param serverIp
     * @param reqTimeout
     * @param terraceType
     * @param companyKey
     */
    public void getEquipmentInfoListData(String serverIp, int reqTimeout, String terraceType, String companyKey, boolean isOfficial) {
        try {
            String companys = "";
            List<ListOrderedMap> companyList = new ArrayList<>();
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select c.company_code, ca.area_code from  company c left join company_area ca on ca.company_code = c.company_code where length(c.company_code) != 0 and c.trusteeship = '1' and c.company_mark_key = ? ";
                companyList = Jdbc.find(findSQL, new Object[]{companyKey});
            } else {
                String findSQL = "select company_code from company where length(company_code) != 0";
                companyList = Jdbc.find(findSQL, new Object[]{});
            }

            //分组逻辑
            int groupSize = 200; //每组的大小
            for (int i = 0; i < companyList.size(); i += groupSize) {
                int end = Math.min(i + groupSize, companyList.size()); //计算当前组的结束索引，避免越界
                if(Constants.TERRACE_TYPE.equals(terraceType)){
                    companys = companyList.subList(i, end).stream().map(t -> t.get("company_code").toString()).distinct().collect(Collectors.joining(","));
                }else{
                    companys = companyList.subList(i, end).stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
                }

                Map map = new HashMap();
                map.put("companys", companys);
                //发起请求
                Connection connect = Jsoup.connect(serverIp + Constants.POST_USE_EQUIPMENT_INFO_LIST_DATA);
                Connection.Response response = connect.data(map)
                        .header("Content-Type", "application/json;utf-8")
                        .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                        .requestBody(JSONObject.toJSONString(map))
                        .maxBodySize(0)
                        .method(Connection.Method.POST)
                        .timeout(reqTimeout)
                        .ignoreContentType(true)
                        .execute();
                String result = response.body();
                if (StringUtils.isNotEmpty(result)) {
                    JSONObject json = JSONObject.parseObject(result);
                    if (json.getInteger("code") == 200) {
                        JSONArray jsonArray = json.getJSONArray("data");
                        if (jsonArray != null && jsonArray.size() > 0) {
                            DataDao.insertEquipmentInfo(jsonArray, companys, terraceType, companyList);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logDevice.info("==> 调用服务接口==> getEquipmentListData发生异常==============》\n" + CommonUtil.e(e));
        }
    }


    /**
     * 获取设备分层数据
     * @param serverIp
     * @param reqTimeout
     * @param terraceType
     * @param companyKey
     * @param isOfficial
     */
    public void getEquipmentLayeredListData(String serverIp, int reqTimeout, String terraceType, String companyKey, boolean isOfficial) {
        try {
            String companys = "";
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select company_code  from  company where length(company_code) != 0 and trusteeship = '1' and company_mark_key = ? ";
                List<ListOrderedMap> list = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = list.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            map.put("companys", companys);
            //发起请求
            HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_EQUIPMENT_LAYERED_LIST_DATA)
                    .body(JSONObject.toJSONString(map), "application/json")
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                    .timeout(reqTimeout)
                    .execute();
            if (httpResponse.getStatus() == 200) {
                JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                if (resultObj.getInteger("code") == 200) {
                    JSONArray jsonArray = resultObj.getJSONArray("data");
                    if (jsonArray != null && jsonArray.size() > 0) {
                        DataDao.insertEquipmentLayered(jsonArray);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logHost.info("==> 调用服务接口==> getEquipmentLayeredListData发生异常==============》" + Constants.LINE_SEPARATOR + CommonUtil.e(e));
        }
    }


    /**
     * 获取温湿度历史数据
     *
     * @param serverIp
     * @param reqTimeout
     * @param mapCompany
     * @param terraceType
     * @param companyKey
     * @param isOfficial
     * @param dataLength
     */
    public void getRecordListData(String serverIp, int reqTimeout, Map<String, ListOrderedMap> mapCompany, String terraceType, String companyKey, boolean isOfficial, String dataLength) {
        try {
            String companys = "";
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select company_code  from  company where length(company_code) != 0 and trusteeship = '1' and company_mark_key = ? ";
                List<ListOrderedMap> list = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = list.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            String trackId = getTrackId(companyKey, Constants.COLL_NAME_RECORD);
            if(StringUtils.isNotEmpty(trackId)) {
                map.put("id",trackId);
                map.put("companys", companys);
                map.put("companyKey", companyKey);
                if (StringUtils.isNotBlank(dataLength) && !"0".equals(dataLength)) {
                    map.put("dataLength", dataLength);
                }
                //发起请求
                HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_RECORD_LIST_DATA)
                        .body(JSONObject.toJSONString(map), "application/json")
                        .header(Header.CONTENT_TYPE, "application/json")
                        .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                        .timeout(reqTimeout)
                        .execute();
                if (httpResponse.getStatus() == 200) {
                    JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                    if (resultObj.getInteger("code") == 200) {
                        JSONArray jsonArray = resultObj.getJSONArray("data");
                        DataDao.insertRecord(jsonArray, mapCompany, companyKey);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logRecord.info("==> 调用服务接口==> getRecordListData发生异常==============》" + Constants.LINE_SEPARATOR + CommonUtil.e(e));
        }
    }


    /**
     * 获取温湿度超限数据
     *
     * @param serverIp
     * @param reqTimeout
     * @param mapCompany
     * @param terraceType
     * @param companyKey
     * @param isOfficial
     * @param dataLength
     */
    public void getCXRecordListData(String serverIp, int reqTimeout, Map<String, ListOrderedMap> mapCompany, String terraceType, String companyKey, boolean isOfficial, String dataLength) {
        try {
            String companys = "";
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select company_code  from  company where length(company_code) != 0 and trusteeship = '1' and company_mark_key = ? ";
                List<ListOrderedMap> list = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = list.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            String trackId = getTrackId(companyKey, Constants.COLL_NAME_RECORD_ALARMS);
            if(StringUtils.isNotEmpty(trackId)) {
                map.put("id", trackId);
                map.put("companys", companys);
                map.put("companyKey", companyKey);
                if (StringUtils.isNotBlank(dataLength) && !"0".equals(dataLength)) {
                    map.put("dataLength", dataLength);
                }
                //发起请求
                HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_CXRECORD_LIST_DATA)
                        .body(JSONObject.toJSONString(map), "application/json")
                        .header(Header.CONTENT_TYPE, "application/json")
                        .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                        .timeout(reqTimeout)
                        .execute();
                if (httpResponse.getStatus() == 200) {
                    JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                    if (resultObj.getInteger("code") == 200) {
                        JSONArray jsonArray = resultObj.getJSONArray("data");
                        DataDao.insertXRecord(jsonArray, mapCompany, companyKey);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logCXRecord.info("==> 调用服务接口==> getCXRecordListData发生异常==============》" + Constants.LINE_SEPARATOR + CommonUtil.e(e));
        }
    }


    /**
     * 获取定位数据
     *
     * @param serverIp
     * @param reqTimeout
     * @param mapCompany
     * @param terraceType
     * @param companyKey
     * @param isOfficial
     * @param dataLength
     */
    public void getGpsListData(String serverIp, int reqTimeout, Map<String, ListOrderedMap> mapCompany, String terraceType, String companyKey, boolean isOfficial, String dataLength) {
        try {
            String companys = "";
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select company_code  from  company where length(company_code) != 0 and trusteeship = '1' and company_mark_key = ? ";
                List<ListOrderedMap> list = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = list.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            String trackId = getTrackId(companyKey, Constants.COLL_NAME_GPS);
            if(StringUtils.isNotEmpty(trackId)) {
                map.put("id", trackId);
                map.put("companys", companys);
                map.put("companyKey", companyKey);
                if (StringUtils.isNotBlank(dataLength) && !"0".equals(dataLength)) {
                    map.put("dataLength", dataLength);
                }
                //发起请求
                HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_GPS_LIST_DATA)
                        .body(JSONObject.toJSONString(map), "application/json")
                        .header(Header.CONTENT_TYPE, "application/json")
                        .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                        .timeout(reqTimeout)
                        .execute();

                if (httpResponse.getStatus() == 200) {
                    JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                    if (resultObj.getInteger("code") == 200) {
                        JSONArray jsonArray = resultObj.getJSONArray("data");
                        DataDao.insertGps(jsonArray, mapCompany, companyKey);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logGps.info("==> 调用服务接口==> getGpsListData发生异常==============》" + Constants.LINE_SEPARATOR + CommonUtil.e(e));
        }
    }

    /**
     * 获取事件数据
     *
     * @param serverIp
     * @param reqTimeout
     * @param mapCompany
     * @param terraceType
     * @param companyKey
     * @param isOfficial
     * @param dataLength
     */
    public void getEventsListData(String serverIp, int reqTimeout, Map<String, ListOrderedMap> mapCompany, String terraceType, String companyKey, boolean isOfficial, String dataLength) {
        try {
            String companys = "";
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select company_code  from  company where length(company_code) != 0 and trusteeship = '1' and company_mark_key = ? ";
                List<ListOrderedMap> list = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = list.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            String trackId = getTrackId(companyKey, Constants.COLL_NAME_EVENTS);
            if(StringUtils.isNotEmpty(trackId)) {
                map.put("id", trackId);
                map.put("companys", companys);
                map.put("companyKey", companyKey);
                if (StringUtils.isNotBlank(dataLength) && !"0".equals(dataLength)) {
                    map.put("dataLength", dataLength);
                }
                //发起请求
                HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_EVENTS_LIST_DATA)
                        .body(JSONObject.toJSONString(map), "application/json")
                        .header(Header.CONTENT_TYPE, "application/json")
                        .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                        .timeout(reqTimeout)
                        .execute();

                if (httpResponse.getStatus() == 200) {
                    JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                    if (resultObj.getInteger("code") == 200) {
                        JSONArray jsonArray = resultObj.getJSONArray("data");
                        DataDao.insertEvents(jsonArray, mapCompany, companyKey);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logGps.info("==> 调用服务接口==> getGpsListData发生异常==============》" + Constants.LINE_SEPARATOR + CommonUtil.e(e));
        }
    }


    /**
     * 查询设备任务数据
     *
     * @param serverIp
     * @param reqTimeout
     * @param terraceType
     * @param companyKey
     */
    public void getTaskLogListData(String serverIp, int reqTimeout, Map<String, ListOrderedMap> mapCompany, String terraceType, String companyKey, boolean isOfficial, String dataLength) {
        try {
            String companys = "";
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select company_code  from  company where length(company_code) != 0 and trusteeship = '1' and company_mark_key = ? ";
                List<ListOrderedMap> list = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = list.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            String trackId = getTrackId(companyKey, Constants.COLL_NAME_RECORD_TASK);
            if(StringUtils.isNotEmpty(trackId)) {
                map.put("id", trackId);
                map.put("companys", companys);
                map.put("companyKey", companyKey);
                if (StringUtils.isNotBlank(dataLength) && !"0".equals(dataLength)) {
                    map.put("dataLength", dataLength);
                }
                //发起请求
                HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_TASK_LOG_LIST_DATA)
                        .body(JSONObject.toJSONString(map), "application/json")
                        .header(Header.CONTENT_TYPE, "application/json")
                        .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                        .timeout(reqTimeout)
                        .execute();

                if (httpResponse.getStatus() == 200) {
                    JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                    if (resultObj.getInteger("code") == 200) {
                        JSONArray jsonArray = resultObj.getJSONArray("data");
                        DataDao.insertRecordTask(jsonArray, mapCompany, companyKey);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logTask.info("==> 调用服务接口==> getTaskLogListData发生异常==============》" + Constants.LINE_SEPARATOR + CommonUtil.e(e));
        }
    }


    /**
     * 查询报修数据信息
     *
     * @param serverIp
     * @param reqTimeout
     * @param terraceType
     * @param companyKey
     */
    public void getRepalrListData(String serverIp, int reqTimeout, String terraceType, String companyKey, boolean isOfficial) {
        try {
            String companys = "";
            //如果是私有化平台，拉取对应key的企业数据，如果是云平台拉取全部数据
            if (Constants.TERRACE_TYPE.equals(terraceType)) {
                String findSQL = "select company_code  from  company where length(company_code) != 0 and trusteeship = '1' and company_mark_key = ? ";
                List<ListOrderedMap> list = Jdbc.find(findSQL, new Object[]{companyKey});
                companys = list.stream().map(t -> t.get("company_code").toString()).collect(Collectors.joining(","));
            }
            Map map = new HashMap();
            map.put("companys", companys);
            HttpResponse httpResponse = HttpRequest.post(serverIp + Constants.POST_USE_REPALR_LIST_DATA)
                    .body(JSONObject.toJSONString(map), "application/json")
                    .header(Header.CONTENT_TYPE, "application/json")
                    .header("type", isOfficial ? Constants.HEADER_TYPE_PROD_ZX_CLOUD2 : Constants.HEADER_TYPE_DEV_ZX_CLOUD2)
                    .timeout(reqTimeout)
                    .execute();

            if (httpResponse.getStatus() == 200) {
                JSONObject resultObj = JSONObject.parseObject(httpResponse.body());
                if (resultObj.getInteger("code") == 200) {
                    JSONArray jsonArray = resultObj.getJSONArray("data");
                    DataDao.insertRepalr(jsonArray);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logRepalr.info("==> 调用服务接口==> getRepalrListData发生异常==============》" + Constants.LINE_SEPARATOR + CommonUtil.e(e));
        }
    }


    /**
     * 查询已经获取到的节点ID
     *
     * @param qiye
     * @param type
     * @return
     */
    public static String getTrackId(String qiye, String type) {
        List<ListOrderedMap> list = Jdbc.find(SqlConstant.SQL_SELECT_TARK_PINGTAI, new Object[]{qiye, type});
        String id = "";
        if (list != null && list.size() > 0) {
            id = list.get(0).get("record_id") + "";
        }
        return id;
    }
}
